package net.meblabs.bookapp.data.dao;

import java.util.List;

import net.meblabs.bookapp.data.model.Author;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
@SuppressWarnings("unchecked")
public class AuthorDaoImpl extends HibernateDaoSupport implements AuthorDao {
	
	@Autowired
	public AuthorDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@Override
	public List<Author> find() {
		Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
		List<Author> authors = session.createQuery("from Author").list();
//		List<Author> authors = getHibernateTemplate().find("from Author");
		return authors;
	}

	@Override
	public Author findById(Long id) {
		Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
		return (Author) session.load(Author.class, id);
//		return getHibernateTemplate().load(Author.class, id);
	}

	@Override
	public void save(Author author) {
//		Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
//		session.saveOrUpdate(author);
		getHibernateTemplate().saveOrUpdate(author);
	}
}
